package com.sss.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sss.springcloud.service.PaymentFeignHystrixService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * @author sss
 * @date 2020-03-05 19:34
 **/
@RestController
@RequestMapping("/consumer")
//全局服务降级defaultFallback 代表回调方法
@DefaultProperties(defaultFallback = "globalFallbackMethod")
public class HystrixFeignController {

    @Resource
    private PaymentFeignHystrixService  paymentService;
    @Value("${server.port}")
    private String  server_port;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String ok(@PathVariable Integer id){
        String ok = paymentService.ok(id);
        return  ok;
    }

    /**
     *   fallbackMethod出场或超时后要调用的方法
     *   commandProperties中的 @HystrixProperty注解中的name代表时间的类型 value设置多长时间为超时
     **/
//    @HystrixCommand(fallbackMethod = "paymentInfo_timeoutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    })
    @GetMapping("/payment/hystrix/timeout/{id}")
    @HystrixCommand //服务降级开启的注解
    public String timeout(@PathVariable Integer id){
        int age=10/0;
        String timeout = paymentService.timeout(id);
        return  timeout;

    }

    public String paymentInfo_timeoutHandler(Integer id){
        return "线程池"+Thread.currentThread().getName()+id+"\t我是80消费者"+"\t(#`O′)~~";
    }

    public String globalFallbackMethod(){
        return "Global全局服务降级方法"+"请稍后再试"+"\t(#`O′)~~";
    }


    @GetMapping("/payment/hystrix/circuit/{id}")
    public String circuit(@PathVariable("id") Integer id){
        return paymentService.circuit(id);
    }

}
